package com.relytosoft.mizar.media.common.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.symmetric.AES;

import java.nio.charset.StandardCharsets;

/**
 * AES加密工具类
 *
 * @Author SunLi
 * @Date: 2022/4/12 16:04
 * @Version 1.0
 */
public class AesUtils {

    /**
     * 密钥
     */
    private static final String BASE64_SECRET = "ddjShwWL3SlH3M7n";
    private static final String IV = "1234567890000000";

    private final static byte[] SECRET_BYTES = BASE64_SECRET.getBytes(StandardCharsets.UTF_8);

    private final static AES SYMMETRIC_AES = new AES(Mode.CBC,Padding.PKCS5Padding,SECRET_BYTES,IV.getBytes(StandardCharsets.UTF_8));

    /**
     * AES加密
     *
     * @param content 待加密字符串
     * @return 返回加密后的十六位进制字符串
     */
    public static String aesEnCode(String content) {
        if (StrUtil.isEmpty(content)) {
            return "";
        }
        return SYMMETRIC_AES.encryptHex(content);
    }

    /**
     * AES解密
     *
     * @param encryptHex 待解密字符串
     * @return 返回解密后得数据
     */
    public static String aesDeCode(String encryptHex) {
        if (StrUtil.isEmpty(encryptHex)) {
            return "";
        }
        return SYMMETRIC_AES.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
    }
}
